home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Snippets / QD3D Juggler / Juggler Sources / CQD3DErrorsDoc.cp < prev    next >
Encoding:
Text File  |  1995-12-27  |  4.1 KB  |  157 lines  |  [TEXT/CWIE]

  1. //
  2. //    CQD3DErrorsDoc.cp
  3. //
  4. //    class CQD3DErrorsDoc
  5. //    Send QuickDraw 3D errors to a TextEdit window.
  6. //
  7. //    by James Jennings
  8. //    November 23, 1995
  9. //
  10.  
  11. #include "CQD3DErrorsDoc.h"
  12. #include "QD3D Debug Macros.h"
  13.  
  14. #include <LTextEdit.h>
  15.  
  16. const PaneIDT    PaneID_TextPane = 9999;
  17. const ResIDT    WIND_ErrorsDoc = 9997;
  18.  
  19. // This is a "singleton" class: There can only be one of them, and that
  20. // instance is stored in the class variable "singleton".
  21. CQD3DErrorsDoc *CQD3DErrorsDoc::singleton = 0;
  22.  
  23.  
  24. CQD3DErrorsDoc::CQD3DErrorsDoc()
  25. {
  26.     ThrowIf_(singleton!=0);    // we can't make more than one of these
  27.     
  28.                             // Create window for our document
  29.     mWindow = LWindow::CreateWindow(WIND_ErrorsDoc, this);
  30.     
  31.                                     // Specify that the text view should
  32.                                     // be the Target when the Window
  33.                                     // is activated
  34.     LTextEdit *pane = (LTextEdit*)mWindow->FindPaneByID(PaneID_TextPane);
  35.     ThrowIfNil_ (pane);
  36.     // SetLatentPane() gives an error: I don't know why.
  37. //    mWindow->SetLatentSub(pane);// assign sub-commander
  38.     
  39.     // Remember the TE pane that we'll be writing to.
  40.     mTE = pane->GetMacTEH();
  41.     ThrowIfNil_ (mTE);
  42.  
  43.     // register our error handlers
  44.     TQ3Status status;
  45.     status = ::Q3Error_Register(ErrorMethod, 0);
  46.     ThrowIfQ3Fail_(status);
  47.     status = ::Q3Warning_Register(WarningMethod, 0);
  48.     ThrowIfQ3Fail_(status);
  49.     status = ::Q3Notice_Register(NoticeMethod, 0);
  50.     ThrowIfQ3Fail_(status);
  51.     
  52.     // make sure we can find this later
  53.     singleton = this;
  54. }
  55.  
  56. CQD3DErrorsDoc::~CQD3DErrorsDoc()
  57. {
  58.  
  59. }
  60.  
  61. //
  62. //    Error handling functions. [static]
  63. //
  64. void 
  65. CQD3DErrorsDoc::ErrorMethod(  TQ3Error first, TQ3Error last, long reference)
  66. {
  67.     TQ3Error dummy;
  68.     OSErr fst, lst;
  69.     CQD3DErrorsDoc *theDoc = CQD3DErrorsDoc::GetInstance();
  70.     if (theDoc) {
  71.         if (Q3Error_IsFatalError(first)) {
  72.             theDoc->Common("\pFATAL ERROR", (long)first, (long)last);
  73.             if (first==kQ3ErrorMacintoshError) {
  74.                 fst = ::Q3MacintoshError_Get(&lst);
  75.                 theDoc->Common("\pFatal Macintosh Error was", (long)fst, (long)lst);
  76.             }
  77.         } else {
  78.             theDoc->Common("\pError", (long)first, (long)last);
  79.             if (first==kQ3ErrorMacintoshError) {
  80.                 fst = ::Q3MacintoshError_Get(&lst);
  81.                 theDoc->Common("\pMacintosh Error was", (long)fst, (long)lst);
  82.             }
  83.         }
  84.         /* This clears the sticky error */
  85.         ::Q3Error_Get(&dummy);
  86.     }
  87. }
  88.  
  89. void 
  90. CQD3DErrorsDoc::WarningMethod(TQ3Warning first, TQ3Warning last, long reference)
  91. {
  92.     CQD3DErrorsDoc *theDoc = CQD3DErrorsDoc::GetInstance();
  93.     if (theDoc) {
  94.         theDoc->Common("\pWarning", (long)first, (long)last);
  95.     }
  96. }
  97.  
  98. void 
  99. CQD3DErrorsDoc::NoticeMethod( TQ3Notice first, TQ3Notice last, long reference)
  100. {
  101.     CQD3DErrorsDoc *theDoc = CQD3DErrorsDoc::GetInstance();
  102.     if (theDoc) {
  103.         theDoc->Common("\pNotice", (long)first, (long)last);
  104.     }
  105. }
  106.  
  107. //
  108. // Common error handling function
  109. //
  110.     static void    // helper function
  111.     DoErrorText(TEHandle hTE, StringPtr type, long err)
  112.     {
  113.         const Str63 del = "\p: ";    // standard delimeter
  114.         Str63 num;
  115.         ::TEInsert(type+1, *type, hTE);    // write Error/Warning/Notice
  116.         ::TEInsert(del+1, *del, hTE);    // write delimeter
  117.         ::NumToString(err, num);
  118.         ::TEInsert(num+1, *num, hTE);    // write error number
  119.         // Get error string and insert it here
  120.         if (short(err) == err) {
  121.             // it's a short error: look up the name in a resource
  122.             ::TEInsert(del+1, *del, hTE);    // write delimeter
  123.             StResource str('Estr', err, false); // don't throw if failed
  124.             if (str.mResourceH) {
  125.                 StHandleLocker strLock(str);
  126.                 ::TEKey('“', hTE);
  127.                 ::TEInsert(*(str.mResourceH)+1, **(str.mResourceH), hTE);
  128.                 ::TEKey('”', hTE);
  129.             } else {
  130.                 Str63 unknown = "\p(no error desc)";
  131.                 ::TEInsert(unknown+1, *unknown, hTE);    // write delimeter
  132.             }
  133.         } else {
  134.             // it's a long error: Do nothing.
  135.         //    ::TEInsert(&err, 4, hTE);
  136.         }
  137.     }
  138.     // end helper function
  139. Boolean
  140. CQD3DErrorsDoc::Common(StringPtr type, long first, long last)
  141. {
  142.     mWindow->Show();    // window is hidden until the first error
  143.     
  144.     Int16 teLength = (**mTE).teLength;
  145.     ::TESetSelect(teLength, teLength, mTE);    // move insertion to the end
  146.     
  147.     DoErrorText(mTE, type, first);
  148.     
  149.     if (last != 0) { // if it's not kQ3ErrorNone, kQ3WarningeNone, or kQ3NoticeNone
  150.         const Str63 lst = "\p: Last ";
  151.         ::TEInsert(lst+1, *lst, mTE);    // write "Last "
  152.         DoErrorText(mTE, type, last);
  153.     }
  154.     TEKey('\r', mTE);    // write newline
  155.     return true;
  156. }
  157.